import { request } from 'bee/utils/request';
import { combineReducers } from 'redux';

const types = {
  // 获取信息
  REQUEST_RISK_DATA: 'REQUEST_RISK_DATA',
  RECIVE_RISK_DATA: 'RECIVE_RISK_DATA',
  REQUEST_DATA_FAIL: 'REQUEST_DATA_FAIL',

  // 投保人信息
  CHANGE_APPLICANT_INFO: 'CHANGE_APPLICANT_INFO',
  // 被保人信息
  CHANGE_INSURER_INFO: 'CHANGE_INSURER_INFO',

  // 险种信息录入
  CHANGE_RISK_INFO: 'CHANGE_RISK_INFO',
};

export const actions = {
  // requestRiskData: async (dispatch, riskId) => {
  //   dispatch({ type: types.REQUEST_RISK_DATA });

  //   const data = await request('./index');
  //   if (data.status === 0) {
  //     dispatch({ type: types.RECIVE_RISK_DATA, data: data.body });
  //   }
  //   dispatch({ type: types.REQUEST_DATA_FAIL, err: data });
  // },
  changeApplicantInfo: info => ({ type: types.CHANGE_APPLICANT_INFO, info }),
  changeInsurerInfo: info => ({ type: types.CHANGE_INSURER_INFO, info }),
  changeRiskInfo: riskInfo => ({ type: types.CHANGE_RISK_INFO, riskInfo }),
};

const initialState = {
  insurerInfo: {
    name: '',
    birth: '',
    sex: '',
    jobCode: '',
  },
  applicantInfo: {
    name: '',
    birth: '',
    sex: '',
    jobCode: '',
    relation: '',
  },
  activeDate: '',
  risks: [
    {
      riskName: '',
      firstYearFee: '',
      table: ['名称', '10000', '1000', '10年'],
    },
  ],
  email: '',
  hasReadNotes: false,
};

const riskReducer = (state = {}, action) => {
  switch (action.type) {
    case types.REQUEST_RISK_DATA:
      return { ...state, isFetching: true };
    case types.RECIVE_RISK_DATA:
      return { ...state, risks: action.data };
    default:
      return state;
  }
};

const insurerInfo = (
  state = {
    name: '',
    birth: '',
    sex: '',
    job: '',
  },
  action
) => {
  switch (action.type) {
    case types.CHANGE_INSURER_INFO:
      return { ...action.info };
    default:
      return state;
  }
};

const applicantInfo = (
  state = {
    name: '',
    birth: '',
    sex: '',
    job: '',
    relation: '',
  },
  action
) => {
  switch (action.type) {
    case types.CHANGE_APPLICANT_INFO:
      return { ...action.info };
    default:
      return state;
  }
};

const changeRiskInfo = (
  state = {
    guaranteePeriod: '', // 保障期间
    feePeriod: '', // 交费期间
    pickPeriod: '', // 领取区间
    supportPlan: '', // 保障计划
    excess: '', // 免赔额
    scale: '', // 赔付比例
    coverage: '', // 保额
    permium: '', // 保费
    // guarantees: [],
    guarantees: [10, 20, 30],
    fees: [10, 20, 30],
    picks: [1, 2, 3, 4],
    supports: [100, 200, 300],
    riskName: '险种名称',
  },
  action
) => {
  switch (action.type) {
    case types.CHANGE_RISK_INFO:
      return { ...action.riskInfo };
    default:
      return state;
  }
};

export const reducers = combineReducers({
  risks: riskReducer,
  insurerInfo,
  applicantInfo,
  riskInfo: changeRiskInfo,
});
